package com.redeemer150.aisuperagent.rag.knowledgebase;


import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 炼金术士
 * @version 1.0
 * @description: 基于阿里云知识库服务的RAG增强顾问（直接使用阿里云百炼平台搭建的RAG向量知识库，直接开启使用省略了本地读取的一步）
 * @date 2025/7/10 10:00
 */

@Configuration
@Slf4j
public class LoveAppCloudRagAdvisorConfig {
    @Value("${spring.ai.dashscope.api-key}")
    private String dashScopeApiKey;

    /**
     * 从阿里百炼中的云知识库检索信息
     * @return
     */
    @Bean
    public Advisor loveAppCloudRagAdvisor() {
        DashScopeApi dashScopeApi= new DashScopeApi(dashScopeApiKey);
        DocumentRetriever retriever = new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder()
                        .withIndexName("恋爱大师-学习版")
                        .build());

        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(retriever)
                .build();
    }
}
